package com.cn.algorithm.two.union;

public class UnionFind_QF extends UnionFind {
    public UnionFind_QF(int capacity) {
        super(capacity);
    }

    @Override
    int find(int v) {
        checkIndex(v);
        return parent[v];
    }

    @Override
    void union(int v1, int v2) {
        int k1 = find(v1);
        int k2 = find(v2);
        if (k1 == k2) {
            return;
        }
        for (int i = 0; i < parent.length; i++) {
            if (parent[i] == k1) {
                parent[i] = k2;
            }
        }
    }
}
